package cn.xx.org.controller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.basic.util.PageList;
import cn.xx.org.domain.SystemDictionaryDetail;
import cn.xx.org.query.SystemDictionaryDetailQuery;
import cn.xx.org.service.ISystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionaryDetail")
@Api(tags = "字典明细接口",description = "字典明细接口详细描述")
//查询单个对象接口：get localhost:8080/systemDictionaryDetail/1
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    // 查询单个对象： get localhost:8080/systemDictionaryDetail/1
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象",notes = "根据id查询")
    public SystemDictionaryDetail findById(@PathVariable("id") Long id) {
        return systemDictionaryDetailService.findById(id);
    }

    //查询所有接口：get localhost:8080/systemDictionaryDetail
    @ApiOperation(value = "查询所有",notes = "")
    @GetMapping()
    public List<SystemDictionaryDetail> findAll() {
        return systemDictionaryDetailService.findAll();
    }


    //添加和修改接口 Put localhost:8080/systemDictionaryDetail
    //从请求体中获取数据：@RequestBody
    @PutMapping
    @ApiOperation(value = "字典明细添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult saveOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        try {
            if (systemDictionaryDetail.getId() == null) {
                systemDictionaryDetailService.save(systemDictionaryDetail);
                System.err.println("增加");
            } else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
                System.err.println("修改fa");
            }
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "根据id删除")
    //删除接口：Delete localhost:8080/systemDictionaryDetail/1
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryDetailService.delete(id);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    //分页查询接口
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<SystemDictionaryDetail> pageQuery(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery) {
        return systemDictionaryDetailService.pageQuery(systemDictionaryDetailQuery);
    }
}
